package cn.dm.service;

import cn.dm.common.Constants;
import cn.dm.common.DateUtil;
import cn.dm.common.EmptyUtils;
import cn.dm.common.MD5;
import cn.dm.mapper.DmAreaMapper;
import cn.dm.mapper.DmUserMapper;
import cn.dm.pojo.DmArea;
import cn.dm.pojo.DmUser;
import cn.dm.vo.TokenVO;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
public class UserService {

    @Resource
    private DmUserMapper dmUserMapper;

    @RequestMapping(value = "/checkLoginByPassword", method = RequestMethod.POST)
    public DmUser checkLoginByPassword(@RequestBody DmUser dmUser) throws Exception{
        return dmUserMapper.checkLoginByPassword(dmUser.getPhone(),dmUser.getPassword());
    }

    @RequestMapping(value = "/getDmUserById", method = RequestMethod.POST)
    public DmUser getDmUserById(@RequestParam("id") Long id) throws Exception{
        return dmUserMapper.getDmUserById(id);
    }

    @RequestMapping(value = "/getDmUserListByMap", method = RequestMethod.POST)
    public List<DmUser> getDmUserListByMap(@RequestBody Map<String, Object> param) throws Exception{
        return dmUserMapper.getDmUserListByMap(param);
    }

    @RequestMapping(value = "/getDmUserCountByMap", method = RequestMethod.POST)
    public Integer getDmUserCountByMap(@RequestBody Map<String, Object> param) throws Exception{
        return dmUserMapper.getDmUserCountByMap(param);
    }

    @RequestMapping(value = "/qdtxAddDmUser", method = RequestMethod.POST)
    public Integer qdtxAddDmUser(@RequestBody DmUser dmUser) throws Exception{
        return dmUserMapper.insertDmUser(dmUser);
    }

    @RequestMapping(value = "/qdtxModifyDmUser", method = RequestMethod.POST)
    public Integer qdtxModifyDmUser(@RequestBody DmUser dmUser) throws Exception{
        return dmUserMapper.updateDmUser(dmUser);
    }

    @RequestMapping(value = "/findByWxUserId", method = RequestMethod.POST)
    public DmUser findByWxUserId(@RequestParam("wxUserId") String wxUserId) throws Exception{
        return dmUserMapper.findByWxUserId(wxUserId);
    }

    @RequestMapping(value = "/createuser", method = RequestMethod.POST)
    public Long createDmUser(@RequestBody DmUser dmUser) throws Exception{
        Integer id=dmUserMapper.insertDmUser(dmUser);
        return Long.valueOf(id);
    }

    @RequestMapping(value = "/generateToken", method = RequestMethod.POST)
    public String generateToken(@RequestBody DmUser user) throws Exception{
//        String token = "DM-" + MD5.getMd5(dmUser.getPhone() + dmUser.getId(), 16);
//        return token;
        //经典面试题：String StringBuilder StringBuffer 的区别
        StringBuffer tokenStr=new StringBuffer();
        tokenStr.append("token:PC-");
        tokenStr.append(MD5.getMd5(user.getPhone(),32));
        tokenStr.append("-");
        tokenStr.append(user.getId());
        tokenStr.append("-");
        tokenStr.append(DateUtil.format(new Date(),"yyyyMMddHHmmss"));
        tokenStr.append("-");
        tokenStr.append(MD5.getRandomCode());
        return tokenStr.toString();
    }

}
